import {saveToStore,getFromStore} from "../util"



const TIMEOUT_HANDLE_NAME = 'TIMEOUT-HANDLE'
const INTERVAL_HANDLE_NAME = "INTERVAL-HANDLE"
export default () => {

    const saveTimeoutHandle = (handle:any) => {
        const d = getFromStore(TIMEOUT_HANDLE_NAME,[]);
        saveToStore(TIMEOUT_HANDLE_NAME,Array.from(new Set([...d,handle])))
    }

    const saveIntervalHandle = (handle:any) => {
        const d = getFromStore(INTERVAL_HANDLE_NAME,[])
        saveToStore(INTERVAL_HANDLE_NAME,Array.from(new Set([...d,handle])))
    }

    const clearHandle = () => {
        let d = getFromStore(TIMEOUT_HANDLE_NAME,[]);
        for(const t of d){
            clearTimeout(t)
        }
        d = getFromStore(INTERVAL_HANDLE_NAME,[])
        for(const t of d){
            clearInterval(t)
        }
        saveToStore(TIMEOUT_HANDLE_NAME,[])
        saveToStore(INTERVAL_HANDLE_NAME,[])
    }

    return {
        save:saveToStore,
        get:getFromStore,
        saveTimeoutHandle,
        saveIntervalHandle,
        clearHandle,
    }
}